package solitaire.presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.* ;

import solitaire.application.TasDeCartes;
import solitaire.controle.CTasDeCartes;

public class PTasDeCartes extends JPanel implements Transferable, java.io.Serializable{

	private static final long serialVersionUID = 1L;
	
	private int nbCartes;
	private int dX;
	private int dY;
	private TasDeCartes controller;
	
	public PTasDeCartes(TasDeCartes controller) {
		nbCartes = 0;
		this.dX = 0;
		this.dY = 10;
		this.controller = controller;
		
		this.setLayout(null);
		
		this.setSize(new Dimension(0, 0));
		this.setPreferredSize(this.getSize());
	}
	
	public PTasDeCartes(int dX, int dY, TasDeCartes controller) {
		nbCartes = 0;
		this.dX = dX;
		this.dY = dY;
		this.controller = controller;
		
		this.setLayout(null);
		
		this.setSize(new Dimension(0, 0));
		this.setPreferredSize(this.getSize());
	}
	
	public TasDeCartes getController() {
		return controller;
	}
	
	public int getNbCartes() {
		return nbCartes;
	}
	
	public void setDX(int dx) {
		this.dX = dx;
	}
	
	public int getDX() {
		return this.dX;
	}
	
	public void setDY(int dy) {
		this.dY = dy;
	}
	
	public int getDY() {
		return this.dY;
	}
	
	public void empiler(PCarte carte) {
		add(carte, 0);
		carte.setLocation(nbCartes*dX, nbCartes*dY);
		this.setSize(new Dimension((PCarte.largeur + nbCartes*dX), (PCarte.hauteur + nbCartes*dY)));
		this.setPreferredSize(this.getSize());
		nbCartes++;
		repaint();
	}
	
	public void depiler(PCarte carte) {
		remove(carte);
		nbCartes--;
		if(nbCartes == 0) {
			this.setSize(new Dimension(0, 0));
		}
		else {
			this.setSize(new Dimension((PCarte.largeur + (nbCartes-1)*dX), (PCarte.hauteur + (nbCartes-1)*dY)));
		}
		this.setPreferredSize(this.getSize());
		repaint();
	}
	
	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
		Object result = null ;
		if (flavor.isMimeTypeEqual (DataFlavor.javaJVMLocalObjectMimeType)) {
			result = this ;
		} else if (flavor.equals(new DataFlavor (PTasDeCartes.class, null))) {
			result = this ;
		} else {
			result = null ;
		}
		return (result) ;
	}

	public DataFlavor[] getTransferDataFlavors() {
		DataFlavor data [] = new DataFlavor [2] ;
		try {
			data [0] = new DataFlavor (DataFlavor.javaJVMLocalObjectMimeType) ;
			data [1] = new DataFlavor (PTasDeCartes.class, null) ;
		} catch (java.lang.ClassNotFoundException e) {
		}
		return (data) ;
	}

	public boolean isDataFlavorSupported(DataFlavor flavor) {
		if ((flavor.isMimeTypeEqual (DataFlavor.javaJVMLocalObjectMimeType))  ||
				(flavor.equals(new DataFlavor (PTasDeCartes.class, null)))) {
			return (true) ;
		} else {
			return (false) ;
		}
	}
	

	public static void main (String args []) {
		JFrame f = new JFrame ("Test PTasDeCarte") ;
		f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		f.getContentPane ().setBackground(new Color(143, 143, 195));

		PCarte c1 = new PCarte ("QH", null);
		c1.setFaceVisible(true);

		PCarte c2 = new PCarte("1D", null);
		c2.setFaceVisible(false);
		
		PCarte c3 = new PCarte("1D", null);
		c3.setFaceVisible(false);
		
		PTasDeCartes ptc = new PTasDeCartes(null);
		
		ptc.empiler(c3);
		//ptc.depiler(c3);
		//ptc.empiler(c2);
		//ptc.empiler(c1);
		ptc.setVisible(true);
		
		f.getContentPane().add(ptc);
		
		f.setVisible (true) ;
		f.pack();
		
	} // main


}
